﻿#region Using References

using System;
using System.Linq;
using Beaker.OpenCube.Specs.Builders;
using Beaker.Services.FileSystem;
using Beaker.Services.FileSystem.Physical;
using Beaker.Services.FileSystem.Zip;
using Beaker.Services.Locator;
using Beaker.Services.Specs.Builders;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FileSystemService = Beaker.Services.FileSystem.FileSystemService;

#endregion

namespace Beaker.Services.Specs
{
	[TestClass]
	public class ZipFileSystemServiceSpecs
	{
		[TestInitialize]
		public void Initialize()
		{
			var manual = new ManualServiceLocator();
			manual.RegisterSingletonService<IFileSystemServiceProvider>( "Zip", new ZipFileSystemServiceProvider() );
			ServiceLocator.SetLocator( manual );
		}

		[TestMethod]
		public void SimpleFileShouldBeFound()
		{
			IFolder folder =
				new FileSystemServiceBuilder()
					.WithFile( "Test", new TextStreamBuilder().WithText( "Content" ).Build() )
					.Build();

			IFile zipFile =
				new ZipFileBuilder()
					.WithFilename( "Foo.zip" )
					.WithFilesFrom( folder )
					.Build();

			// act
			IFileSystemService zip = FileSystemService.FromFile( zipFile );

			// assert
			zip.GetFiles()
				.Should().Contain( f => f.Name == "Test" );
		}

		[TestMethod]
		public void FileInFolderShouldBeFoundInFolder()
		{
			IFolder folder =
				new FileSystemServiceBuilder()
					.WithFile( "Folder/File", new TextStreamBuilder().WithText( "Content" ).Build() )
					.Build();

			IFile zipFile =
				new ZipFileBuilder()
					.WithFilename( "foo.zip" )
					.WithFilesFrom( folder )
					.Build();

			// act
			IFileSystemService zip = FileSystemService.FromFile( zipFile );

			// assert
			zip.GetFolder( "Folder" ).GetFiles()
				.Should().Contain( f => f.Name == "File" );
		}
	}
}
